import { queryGrade, addGrade, deleteGrade, updateGrade } from '../services/grade';

export default {
  namespace: 'grade',

  state: {
    courseGrade: [],
  },

  effects: {
    *fetchCourseGrade({ payload = {} }, { call, put }) {
      const response = yield call(queryGrade, payload);
      if (response.status) {
        yield put({
          type: 'saveCourseGrade',
          payload: response.data,
        });
      }
    },

    *updateCourseGrade({ payload = {} }, { call, put }) {
      const response = yield call(updateGrade, payload);
      if (response.status) {
        const dataLsit = yield call(queryGrade, payload);
        yield put({
          type: 'saveCourseGrade',
          payload: dataLsit,
        });
      }
    },

    *deleteCourseGrade({ payload = {} }, { call, put }) {
      const response = yield call(deleteGrade, payload);
      if (response.status) {
        const dataLsit = yield call(queryGrade, payload);
        yield put({
          type: 'saveCourseGrade',
          payload: dataLsit,
        });
      }
    },

    *addCourseGrade({ payload = {} }, { call, put }) {
      const response = yield call(addGrade, payload);
      if (response.status) {
        const dataLsit = yield call(queryGrade, payload);
        yield put({
          type: 'saveCourseGrade',
          payload: dataLsit,
        });
      }
    },
  },
  reducers: {
    saveCourseGrade(state, { payload }) {
      return {
        ...state,
        courseGrade: payload,
      };
    },
  },
};
